import {Component, ViewChild} from '@angular/core';
import {Content, InfiniteScroll, IonicPage, ModalController, NavController, NavParams} from 'ionic-angular';
import {StorageProvider} from "@providers/storage";
import {PagingConfig} from "@providers/paging";
import {CustomerProvider} from "@providers/interface/customer";
import {CommonProvider} from "@providers/common";

/**
 * Generated class for the CustomerServerListPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-customer-server-list',
  templateUrl: 'customer-server-list.html',
})
export class CustomerServerListPage {
  @ViewChild(Content) content: Content;
  userInfo: any = this.storage.read('userInfo');
  isScrollDown = false;//判断页面是否是向下滑动

  //全部订单分页配置
  AllsConfig: PagingConfig = new PagingConfig();
  Alls: any[] = [];

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private modalCtrl: ModalController,
              private storage: StorageProvider,
              private common: CommonProvider,
              private customer: CustomerProvider) {
  }

  ionViewWillEnter() {
    this.Alls = [];
    this.AllsConfig = new PagingConfig();
    this.selectAll();
  }

  push(paage: string, data?: any) {
    this.navCtrl.push(paage, data)
  }

  /**
   * 刷新数据
   */
  refresh(refresher?: any) {
    this.AllsConfig = new PagingConfig();
    this.getData(data => {
      this.Alls = data;
      if (refresher) {
        setTimeout(() => {
          refresher.complete();
        }, 500);
      }
    }, this.AllsConfig, '2', error2 => {
      if (refresher) {
        setTimeout(() => {
          refresher.complete();
        }, 500);
      }
    });
  }

  /**
   * 查询所有
   */
  selectAll() {
    if (this.Alls.length == 0) {
      this.getData((value) => {
        this.Alls = value;
        console.log(this.Alls);
      }, this.AllsConfig, '2');
    }
  }

  /**
   * 全部订单上滑刷新事件
   */
  getMoreAlls(infiniteScroll: InfiniteScroll) {
    this.getData((value) => {
      infiniteScroll.complete();
      if (this.AllsConfig.isEnd) {
        infiniteScroll.enable(false);
      }
      this.Alls.push.apply(this.Alls, value);
    }, this.AllsConfig, '2');
  }

  /**
   * 评价
   * @param data
   */
  doComment(data: any) {
    let modal = this.modalCtrl.create("CommentPage", data);
    modal.onDidDismiss(res => {
      if (res) {
        data.SFPJ = '1';
      }
    });
    modal.present();
  }

  /**
   *查看师傅位置
   * @param data
   */
  map(data: any) {
    let modal = this.modalCtrl.create("OrderMapPage", data);
    modal.onDidDismiss(res => {
      if (res) {
      }
    });
    modal.present();
  }


  /**
   * 获取数据
   * @param WCZT 状态 -1 未完成   1 完成   2 所有订单
   * @param pagingConfig 分页
   * @param fn 方法
   */
  private getData(fn: (value) => void, pagingConfig, WCZT?: string, errorFun?: (error) => void) {
    if (pagingConfig.isEnd) {
      return;
    }
    this.customer.getMyServerOrders({
      'PARTYID': this.userInfo["PARTYID"],
      'WCZT': WCZT,
    }, pagingConfig)
      .then((data) => {
        fn(data);
        console.info(data);
      }).catch((error) => {
      if (errorFun) {
        errorFun(error);
      }
    });
  }

  contact(party_id) {
    this.common.startChat([{code: party_id}]);
  }
}
